<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use DB, Cache;
use App\Models\Cashier\Sale;
use App\Models\Shop\Customer;

class EchartController extends Controller
{
    //本周起止时间unix时间戳
    private $week_start;
    private $week_end;

    //本月起止时间unix时间戳
    private $month_start;
    private $month_end;

    function __construct()
    {

        $this->week_start = mktime(0, 0, 0, date("m"), date("d") - date("w") + 1, date("Y"));
        $this->week_end = mktime(23, 59, 59, date("m"), date("d") - date("w") + 7, date("Y"));

        $this->month_start = mktime(0, 0, 0, date("m"), 1, date("Y"));
        $this->month_end = mktime(23, 59, 59, date("m"), date("t"), date("Y"));
    }

    /**
     * 本周销售额
     * @return mixed
     */
    public function week_sale()
    {
//        return Cache::remember('xApi_visualization_sales_amount', 60, function () {
        $amount = [];
        for ($i = 0; $i < 7; $i++) {
            $start = date('Y-m-d H:i:s', strtotime("+" . $i . " day", $this->week_start));
            $end = date('Y-m-d H:i:s', strtotime("+" . ($i + 1) . " day", $this->week_start));
            $amount['cache'][] = Sale::whereBetween('created_at', [$start, $end])->where('pay_type', 1)->sum('total_price');
            $amount['wechat'][] = Sale::whereBetween('created_at', [$start, $end])->where('pay_type', 2)->sum('total_price');
            $amount['alipay'][] = Sale::whereBetween('pay_time', [$start, $end])->where('pay_type', 3)->sum('total_price');
            $amount['jxyb'][] = Sale::whereBetween('created_at', [$start, $end])->where('pay_type', 4)->sum('total_price');
            $amount['whyb'][] = Sale::whereBetween('pay_time', [$start, $end])->where('pay_type', 5)->sum('total_price');
        }

        $data = [
            'week_start' => date("Y年m月d日", $this->week_start),
            'week_end' => date("Y年m月d日", $this->week_end),
            'amount' => $amount,
        ];
        return $data;
//        });
    }


    /**
     * 商城系统---会员性别及人数统计
     * @return array
     */
    public function sex_total()
    {
        $data = [
            ['value' => Customer::where('sex', 1)->count(), 'name' => '男'],
            ['value' => Customer::where('sex', 2)->count(), 'name' => '女']
        ];

        return $data;

    }


    /***
     * 商城会员省份及人数统计
     * @return mixed
     */
    public function province_total()
    {
        $customer_total = DB::table('customers')
            ->select(DB::raw('province as name, count(*) as value'))
            ->groupBy('province')->get();
        return $customer_total;
    }


    /***
     * 商城支付方式统计
     * @return array
     */
    public function pay_type()
    {
        $data = [
            ['name' => '现金', 'value' => Sale::where('pay_type', '1')->count()],
            ['name' => '微信', 'value' => Sale::where('pay_type', '2')->count()],
            ['name' => '支付宝', 'value' => Sale::where('pay_type', '3')->count()],
            ['name' => '武汉医保', 'value' => Sale::where('pay_type', '4')->count()],
            ['name' => '江夏医保', 'value' => Sale::where('pay_type', '5')->count()],
        ];

        return $data;
    }
}
